import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/device/lock/api.dart";
import "package:ct_pa_majordomo/pages/device/lock/fingerprint/widgets/expiration_date/index.dart";
import "package:ct_pa_majordomo/pages/device/lock/fingerprint/widgets/ope_record/index.dart";
import "package:ct_pa_majordomo/pages/widgets/menu_block.dart";
import "package:ct_pa_majordomo/router/keys.dart";
import "package:ct_pa_majordomo/utils/date_tools.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:ct_pa_majordomo/widgets/button/index.dart";
import "package:ct_pa_majordomo/widgets/tai_dialog/index.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";
import "package:ttlock_flutter/ttlock.dart";

class Detail extends StatelessWidget {
  final Map info;
  const Detail({super.key, required this.info});

  void _removeLocalFingerprint() {
    if (info["fingerprintNumber"] != null) {
      TTLock.deleteFingerprint(info["fingerprintNumber"], info["lockData"], () {
        EasyLoading.showToast("删除成功");
      }, (errorCode, errorMsg) {
        EasyLoading.showError("$errorCode, $errorMsg");
      });
    } else {
      EasyLoading.showError("指纹码不存在");
    }
  }

  @override
  Widget build(BuildContext context) {
    Future<void> delFingerprint(int fingerprintId) async {
      /// 删除指纹
      try {
        EasyLoading.show();
        await lockApi.deleteFingerprint(info["lockId"], fingerprintId);
        EasyLoading.showToast("删除指纹成功");
        Stacker.pop(true);
      } catch (error) {
        print("error: $error");
      }
    }
    return Scaffold(
      appBar: AppBar(
        iconTheme: const IconThemeData(color: Colors.white),
        title: const Text("指纹详情", style: TextStyle(color: Colors.white),),
        centerTitle: true,
        backgroundColor: ThemeVars.colorPrimaryDark,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios_new),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
      body: ListView(
        children: [
          MenuBlock(
            borderRadius: const BorderRadius.all(Radius.circular(0)),
            items: [
              MenuBlockItem(
                text: "指纹号",
                showArrow: false,
                suffix: Text(info["fingerprintNumber"], style: TextStyle(color: ThemeVars.colorTextTer),),
              ),
              MenuBlockItem(
                text: "姓名",
                showArrow: false,
                suffix: Text(info["fingerprintName"], style: TextStyle(color: ThemeVars.colorTextTer),),
              ),
              MenuBlockItem(
                text: "有效期",
                suffix: Column(
                  children: [
                    Text(DateTools.formatYMD(info["startDate"]), style: TextStyle(color: ThemeVars.colorTextTer),),
                    Text(DateTools.formatYMD(info["endDate"]), style: TextStyle(color: ThemeVars.colorTextTer),),
                  ],
                ),
                onTap: () {
                  Navigator.push(
                    context,
                    CupertinoPageRoute(
                      builder: (BuildContext context) {
                        return const ExpirationDate();
                      },
                    ),
                  );
                }
              )
            ],
          ),
          const SizedBox(height: 12,),
          MenuBlock(
            borderRadius: const BorderRadius.all(Radius.circular(0)),
            items: [
              MenuBlockItem(
                text: "添加者",
                showArrow: false,
                suffix: Text(info["senderUsername"], style: TextStyle(color: ThemeVars.colorTextTer),),
              ),
              MenuBlockItem(
                text: "添加时间",
                showArrow: false,
                suffix: Text(DateTools.formatYMD(info["createDate"], format: "{y}-{m}-{d} {h}:{i}:{s}"), style: TextStyle(color: ThemeVars.colorTextTer),),
              ),
            ],
          ),
          const SizedBox(height: 12,),
          MenuBlock(
            borderRadius: const BorderRadius.all(Radius.circular(0)),
            items: [
              MenuBlockItem(
                text: "操作记录",
                onTap: () {
                  Navigator.push(
                    context,
                    CupertinoPageRoute(
                      builder: (BuildContext context) {
                        return OpeRecord(info: info);
                      },
                    ),
                  );
                },
              ),
            ],
          ),
          Padding(
            padding: EdgeInsets.fromLTRB(ThemeVars.paddingPage, 40, ThemeVars.paddingPage, 0),
            child: Button(
              radius: 5,
              type: ButtonType.danger,
              onPressed: () {
                taiDialog(
                  context: context,
                  onConfirm: (taiContext) {
                    Stacker.pop();
                    delFingerprint(info["fingerprintId"]);
                  },
                  child: const Text("确定删除指纹？", textAlign: TextAlign.center,),
                );
              },
              child: const Text("删除"),
            ),
          ),
        ],
      ),
    );
  }

}